package com.lagou

import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.{DataFrame, SparkSession}

object WorkSix {
  def main(args: Array[String]): Unit = {
    Logger.getLogger("org").setLevel(Level.WARN)
    val spark: SparkSession = SparkSession.builder()
      .appName(s"${this.getClass.getCanonicalName}")
      .master("local[*]")
      .getOrCreate()

    val tab = List((1, "2019-03-04", "2020-02-03"), (2, "2020-04-05", "2020-08-04"), (3, "2019-10-09", "2020-06-11"))

    val df: DataFrame = spark.createDataFrame(tab).toDF("id", "startDate", "endDate")

    val flatDf = df.select("startDate").union(df.select("endDate")).toDF("date")

    flatDf.createOrReplaceTempView("allDate")

    spark.sql(
      """
        |select date startDate, nvl(lead(date) over(order by date), date) endDate from allDate
        |""".stripMargin).show()

    spark.close()
  }
}
